<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2018/12/14
 * Time: 16:41
 */

namespace api\storage\model;


use think\Model;

/**
 * 解除仓储员和仓库的绑定
 * Class ShelvesModel
 * @package api\storage\model
 */
class ShelvesModel extends Model
{
    protected $table = 'iwh_shelves';
    protected $pk = 'uuid';

    public function Seat(){
        return $this->hasMany('SeatModel','shelves_uuid','uuid');
    }

    public function updateStaffUuid($staffUuid = '', $uuid = '', $save = '')
    {
        $where = ['staff_uuid' => $staffUuid, 'uuid' => $uuid];
        $result = $this->where($where)->save(['staff_uuid' => $save]);
        return $result;
    }

    public function updateUserId($uuid,$userId = ''){
        return $this->isUpdate(true)->save(['user_id'=>$userId],['uuid'=>$uuid]);
    }

    /**
     * 更新flag字段
     *
     * @param $code
     * @param $storageUuid
     * @param $flag
     * @return false|int
     */
    public function updateFlag($code,$storageUuid,$flag){
        $where['shelves_code'] = $code;
        $where['storage_uuid'] = $storageUuid;
        $data['flag'] = $flag;
        return $this->isUpdate(true)->save($data,$where);
    }

    /**
     * 根据用户查找货架
     *
     * @param $userId
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function findByUserId($userId){
        return $this->where('user_id',$userId)->find();
    }

    /**
     * 根据userId 和 storageUuid 来查找货架
     *
     * @param $userId
     * @param $uuid
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function findByUserIdAndUuid($userId,$uuid){
        return $this->where('user_id',$userId)->where('storage_uuid',$uuid)->find();
    }

    /**
     * 根据uuid查询数据
     *
     * @param $uuid
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function findByUuid($uuid = ''){
        return $this->where('uuid',$uuid)->find();
    }

    /**
     * 查找可用站位
     *
     * @param string $type
     * @param string $storageUuid
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function findSeatCodeWithSeat($type = '',$storageUuid = ''){
        $where['s.status'] = 0;
        $where['s.is_used'] = 0;
        $where['sh.type'] = $type;
        $where['sh.storage_uuid'] = $storageUuid;
        return $this->alias('sh')
            ->join(['iwh_seat'=>'s'],'sh.uuid = s.shelves_uuid','left')
            ->field('s.seat_code,s.seat_id,sh.shelves_code,sh.uuid as shelves_uuid')
            ->where($where)
            ->order('sh.shelves_code asc,s.seat_code asc')
            ->find();
    }

}
